package org.example.mq.mqserver.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.mq.mqserver.coreenum.ExchangeType;

import java.util.HashMap;
import java.util.Map;

public class Exchange {
    //唯一身份标识
    private String name;
    //交换机类型
    private ExchangeType type = ExchangeType.DIRECT;
    //是否持久化
    private boolean durable = false;
    //是否自动删除
    private boolean autoDelete = false;
    //提供额外的选项
    private Map<String, Object> arguments = new HashMap<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ExchangeType getType() {
        return type;
    }

    public void setType(ExchangeType type) {
        this.type = type;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

/*    public Map<String, Object> getArguments() {
        return arguments;
    }

    public void setArguments(Map<String, Object> argument) {
        this.arguments = argument;
    }*/


//    以字符串形式的json格式输出
    public String getArguments(){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{}";
    }


//    输入为字符串但是设置为json格式
    public void setArguments(String argumentsJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String,Object>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

//    提供一组getter setter方法用户设置这里的键值对和取出键值对

    public Object getArguments(String key){
        return arguments.get(key);
    }
    public void setArguments(String key,Object value){
        arguments.put(key, value);
    }

    public void setArguments(Map<String,Object> arguments){
        this.arguments = arguments;
    }
}
